﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;
using System.Linq;

namespace LM.YF.K3Cloud.Plugin
{
    /// <summary>
    /// 运输路线表单插件
    /// </summary>
    /// <remarks>
    /// 功能：1、自动获取承运商序号2、获取运输路线名称；3、获取运输路线编码
    /// 创建人：lxl
    /// 创建日期：2023-12-27
    /// 修改信息（1）：修改人，修改日期，修改内容
    /// </remarks>
    [HotUpdate, Description("运输路线表单插件")]
    public class TransfRuteBillPlugin : AbstractBillPlugIn
    {
        public override void AfterBindData(EventArgs e)
        {
            base.AfterBindData(e);
            //新建状态时修改
            if (View.OpenParameter.Status.Equals(OperationStatus.ADDNEW))
            {
                GetSupplySeq();
            }
        }

        /// <summary>
        /// 字段变化
        /// </summary>
        /// <param name="e"></param>
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            switch (e.Field.Key.ToUpper())
            {
                case "F_RDLV_START_POINT"://起运点
                    GetName();
                    GetSupplySeq();
                    break;
                case "F_RDLV_DESTION"://运输区域
                    GetName();
                    GetSupplySeq();
                    break;
                case "F_RDLV_CARRIER"://承运商
                    GetName();
                    GetSupplySeq();
                    break;
                case "F_RDLV_PRICETYPE"://计价方式
                    GetName();
                    break;
                default: break;
            }
        }

        /// <summary>
        /// 获取名称，名称=起运点-运输区域-承运商-计价方式
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void GetName()
        {
            // 获取起运点
            var startPointName = (View.Model.GetValue("F_RDLV_start_point") as DynamicObject)?["name"].ToString();

            //获取运输目的地
            DynamicObject F_RDLV_Destion = this.View.Model.GetValue("F_RDLV_Destion") as DynamicObject;
            string F_RDLV_DestionName = F_RDLV_Destion?["name"].ToString();
            //承运商
            DynamicObject F_RDLV_Carrier = this.View.Model.GetValue("F_RDLV_Carrier") as DynamicObject;
            string F_RDLV_CarrierName = F_RDLV_Carrier?["name"].ToString();
            //计价方式
            var F_RDLV_PriceType = this.View.Model.GetValue("F_RDLV_PriceType");
            string F_RDLV_PriceTypeValue = F_RDLV_PriceType?.ToString();//计价方式值

            Field field = this.View.BusinessInfo.GetField("F_RDLV_PriceType");
            ComboField comboField = field as ComboField;
            var enumObj = (EnumObject)comboField.EnumObject;
            //根据枚举值获取枚举项，然后拿枚举项的枚举名称
            string F_RDLV_PriceTypeName =
            enumObj.Items.FirstOrDefault(
            p =>
            p.Value.ToString().Equals(F_RDLV_PriceTypeValue.Trim())).Caption.ToString();
            string fname = startPointName + "至" + F_RDLV_DestionName + "-" + F_RDLV_CarrierName + "-" + F_RDLV_PriceTypeName;
            this.View.Model.SetValue("fname", fname);
        }

        /// <summary>
        /// 获取承运商流水号
        /// </summary>
        private void GetSupplySeq()
        {
            string F_RDLV_SupplySeq = "";
            //获取运输计划点
            DynamicObject F_RDLV_start_point = this.View.Model.GetValue("F_RDLV_start_point") as DynamicObject;
            //获取运输目的地
            DynamicObject F_RDLV_Destion = this.View.Model.GetValue("F_RDLV_Destion") as DynamicObject;
            //当前路线主键
            string fid = this.View.Model.GetPKValue()?.ToString() ?? "0";

            if (F_RDLV_start_point != null && F_RDLV_Destion != null)
            {
                string F_RDLV_start_pointId = Convert.ToString(F_RDLV_start_point["id"]);
                string F_RDLV_DestionId = Convert.ToString(F_RDLV_Destion["id"]);
                //获取运输计划点-运输目的地 最大的承运商
                string strSql = string.Format(@"select max(F_RDLV_SupplySeq) F_RDLV_SupplySeq
                    from RDLV_t_TransfRute 
                    where F_RDLV_START_POINT = {0}
                        and F_RDLV_Destion={1}
                        and fid <>{2}", F_RDLV_start_pointId, F_RDLV_DestionId, fid);
                DynamicObjectCollection resultData = DBUtils.ExecuteDynamicObject(this.Context, strSql);
                if (resultData != null && resultData.Count > 0)
                {
                    if (Convert.ToString(resultData[0]["F_RDLV_SupplySeq"]).ToString() != "")
                    {
                        char tempValue = Convert.ToChar(resultData[0]["F_RDLV_SupplySeq"]);
                        F_RDLV_SupplySeq = Convert.ToChar(1 + tempValue).ToString();
                    }
                    else
                    {
                        F_RDLV_SupplySeq = "A";
                    }
                }
                else
                { F_RDLV_SupplySeq = "A"; }
                this.View.Model.SetValue("F_RDLV_SupplySeq", F_RDLV_SupplySeq);
            }
        }
    }
}
